/**
 * 漆水河 脚手架基础公开课
 * 2022/8/13 --- 2022/12/13
 * 有疑问可加QQ群: 809126489，后续会持续输出与大家一起成长
 */
package com.qishuihe.common.domain.user.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qishuihe.baseinterface.constant.CommonConstant;
import com.qishuihe.common.domain.user.service.UserService;
import com.qishuihe.common.domain.user.entity.UserEntity;
import com.qishuihe.common.domain.user.mapper.UserMapper;

import com.qishuihe.baseinterface.basevo.vo.ResultVO;
import com.qishuihe.baseinterface.basevo.vo.ResultPageVO;
import com.qishuihe.outbound.user.QueryPageDTO;
import com.qishuihe.outbound.user.RegisteredUserDTO;
import com.qishuihe.baseinterface.utils.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;



@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public ResultVO<RegisteredUserDTO> modifyUser(RegisteredUserDTO registeredUserVO) {
        ResultVO<RegisteredUserDTO> resultDto =  new ResultVO<RegisteredUserDTO>();
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(registeredUserVO,userEntity);
        // 判断是否修改用户密码，为空就是不修改，不为空就是修改，对明文加密
        if(!StringUtil.isEmpty(userEntity.getPassword())){
            userEntity.setPassword(passwordEncoder.encode(userEntity.getPassword()));
        }
        if(!super.saveOrUpdate(userEntity)){
            resultDto.setCode(CommonConstant.STATUS_ERROR);
            resultDto.setMessage("修改用户失败");
            resultDto.setObj(registeredUserVO);
        }else {
            userEntity.setPassword(null);
            BeanUtils.copyProperties(userEntity, registeredUserVO);
            resultDto.setObj(registeredUserVO);
        }
        return resultDto;
    }

    @Override
    public ResultPageVO<RegisteredUserDTO> queryUser(QueryPageDTO<RegisteredUserDTO> queryPageDTO) {
        RegisteredUserDTO qegisteredUserVO = queryPageDTO.getParamObj();
        Page<UserEntity> userEntityPage= new Page<>();
        BeanUtils.copyProperties(queryPageDTO,userEntityPage);
        LambdaQueryWrapper<UserEntity> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(!StringUtil.isEmpty(qegisteredUserVO.getUsername())) {
            lambdaQueryWrapper.like(UserEntity::getUsername, qegisteredUserVO.getUsername());
        }
        if(!StringUtil.isEmpty(qegisteredUserVO.getEmail())) {
            lambdaQueryWrapper.like(UserEntity::getEmail, qegisteredUserVO.getEmail());
        }
        if(!StringUtil.isEmpty(qegisteredUserVO.getWorkId())) {
            lambdaQueryWrapper.like(UserEntity::getWorkId,qegisteredUserVO.getWorkId());
        }

        userEntityPage =super.page(userEntityPage,lambdaQueryWrapper);
        ResultPageVO<RegisteredUserDTO> resultPageVO = new ResultPageVO<>();
        BeanUtils.copyProperties(userEntityPage, resultPageVO);
        return resultPageVO;
    }

}
